package com.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @author : ranzlupup
 * @date : 2023/3/1 16:14
 */
object RDD_Transform_mapPartitionsWithIndex {
    def main(args: Array[String]): Unit = {
        val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
        val sparkContext: SparkContext = new SparkContext(sparkConf)
        val RDD: RDD[Int] = sparkContext.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8), 2)

        // TODO RDD转换算子
        //      Value类型
        //      mapPartitionsWithIndex

        RDD.mapPartitionsWithIndex(
            (parIndex, iter) => {
                iter.map(
                    item => (item, parIndex)
                )
            }
        ).collect().foreach(println)

        sparkContext.stop()
    }
}
